package com.hzsk.lisence.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzsk.lisence.config.AnnotationForLog;
import com.hzsk.lisence.config.myexception.OperateException;
import com.hzsk.lisence.entity.*;
import com.hzsk.lisence.entity.query.LisenceServiceQuery;
import com.hzsk.lisence.entity.response.IncResponce;
import com.hzsk.lisence.mapper.HardwareMapper;
import com.hzsk.lisence.mapper.LisenceLogMapper;
import com.hzsk.lisence.mapper.LisenceUserMapper;
import com.hzsk.lisence.service.HardwareService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class HardwareServiceImpl implements HardwareService {

    @Resource
    private HardwareMapper hardwareMapper;

    @Resource
    private LisenceUserMapper lisenceUserMapper;

    @Override
    @AnnotationForLog(operationType = LogEnum.CREATE_HARDWARE,operation = "创建硬件信息",operationalEntity = "HardwareEntity.java/STUDENT_T")
    public void createHardware(HardwareEntity hardwareEntity, Integer userId, IncResponce incResponce)  {
        Integer groupId = lisenceUserMapper.getUserGroupId(userId);
        String key = "";
        String hardwareName = hardwareEntity.getHardwareName();
        if(hardwareEntity.getMacKey().length()!=0) {
            String macKey = hardwareEntity.getMacKey();
            String cpukey = hardwareEntity.getCpuKey();
            String hardDiskKey = hardwareEntity.getHardDiskKey();
            if (StringUtils.isBlank(macKey) || StringUtils.isBlank(cpukey) || StringUtils.isBlank(hardDiskKey)){
                incResponce.setCode(1);
                incResponce.setErrorMessage("请求参数异常");
                return;
            }
            key = cpukey + "," + hardDiskKey + "," + cpukey;
        }
        else if (hardwareEntity.getHardwareKey().length() != 0) {
            key = hardwareEntity.getHardwareKey();
        }
        else
        {
            incResponce.setCode(1);
            incResponce.setErrorMessage("请选择输入方式");
            return;
        }
        hardwareEntity.setHardwareKey(key);
        List<HardwareEntity> hardwareEntities = hardwareMapper.selectHardwareNameByKey(key);
        if (hardwareEntities.size() > 0) {
            incResponce.setCode(1);
            incResponce.setErrorMessage("硬件信息重复");
            return;
        }
        List<HardwareEntity> names = hardwareMapper.selectHardwareNameByName(hardwareName);
        if (names.size() > 0) {
            incResponce.setCode(1);
            incResponce.setErrorMessage("硬件名称不能重复");
            return;
        }
        hardwareMapper.saveHardware(hardwareEntity);
//        Integer id = hardwareEntity.getId();
//        List<Integer> softwareIds = hardwareEntity.getSoftwareIds();
//        if (softwareIds != null && softwareIds.size()>0) {
//            for (Integer softwareId : softwareIds) {
//                HardwareConnSoftware hardwareConnSoftware = new HardwareConnSoftware();
//                hardwareConnSoftware.setHardwareId(id);
//                hardwareConnSoftware.setSoftwareId(softwareId);
//                hardwareMapper.saveConnSoftware(hardwareConnSoftware);
//            }
//        }
        incResponce.setCode(0);
        incResponce.setData("操作成功");
    }

    @Override
    @AnnotationForLog(operationType = LogEnum.CREATE_HARDWARE_GROUPING,operation = "创建硬件分组信息",operationalEntity = "HardwareGroupingEntity.java/STUDENT_T")
    public void createHardwareGrouping(HardwareGroupingEntity hardwareGroupingEntity, Integer userId,IncResponce incResponce) {
        List<HardwareGroupingEntity> hardwareGroupingEntities = hardwareMapper.selectHardwareGroupingByName(hardwareGroupingEntity.getHardwareGroupingName());
        if (hardwareGroupingEntities.size() > 0 ) {
            incResponce.setErrorMessage("硬件分组名称重复");
            incResponce.setCode(1);
            return;
        }
        hardwareMapper.createHardwareGrouping(hardwareGroupingEntity);
        incResponce.setData("操作成功");
        incResponce.setCode(0);
    }

    @Override
    public Object selectHardwareGrouping() {
        return hardwareMapper.selectHardwareGrouping();
    }

    @Override
    public Object selectHardwareGroupingInfo(LisenceServiceQuery lisenceServiceQuery) {
        PageHelper.startPage(lisenceServiceQuery.getPageNum(), lisenceServiceQuery.getPageSize());
        List<HardwareEntity> infos =  hardwareMapper.selectHardwareGroupingInfo(lisenceServiceQuery);
        return new PageInfo<>(infos);
    }

    @Override
    public Object selectHardwareToBeGrouped(LisenceServiceQuery lisenceServiceQuery) {
        Integer id = lisenceServiceQuery.getId();
        List<Integer> integers = hardwareMapper.selectHardIdsByGroupingId(id);
        PageHelper.startPage(lisenceServiceQuery.getPageNum(), lisenceServiceQuery.getPageSize());
        List<HardwareEntity> hardwareEntities = hardwareMapper.selectHardwareByNotIds(integers);
        return new PageInfo<>(hardwareEntities);

    }

    @Override
    @AnnotationForLog(operationType = LogEnum.UPDATE_HARDWARE_GROUPING,operation = "修改硬件分组信息",operationalEntity = "HardwareGroupingEntity.java/STUDENT_T")
    public void updateHardwareGrouping(HardwareGroupingEntity hardwareGroupingEntity, Integer userId, IncResponce incResponce) {
        List<HardwareGroupingEntity> hardwareGroupingEntities = hardwareMapper.selectHardwareGroupingByName(hardwareGroupingEntity.getHardwareGroupingName());
        if (hardwareGroupingEntities.size() >= 1 && !Objects.equals(hardwareGroupingEntities.get(0).getId(), hardwareGroupingEntity.getId())) {
            incResponce.setData("硬件分组名称重复");
            incResponce.setCode(1);
            return;
        }
        hardwareMapper.updateHardwareGrouping(hardwareGroupingEntity);
        incResponce.setData("操作成功");
        incResponce.setCode(0);
    }

    @Override
    @AnnotationForLog(operationType = LogEnum.DEL_HARDWARE_GROUPING,operation = "删除硬件分组信息",operationalEntity = "HardwareGroupingEntity.java/STUDENT_T")
    public String delHardwareGrouping(Integer id, Integer userId) {
        hardwareMapper.delHardwareGrouping(id);
        hardwareMapper.delHardwareGroupingConn(id);
        return "操作成功";
    }

    @Override
    public String setSoftwareConnGrouping(Integer id, int[] ids) {
        for (int i : ids) {
            HardwareConnHardwareGrouping conn = new HardwareConnHardwareGrouping();
            conn.setHardwareId(i);
            conn.setHardwareGroupingId(id);
            hardwareMapper.setSoftwareConnGrouping(conn);
        }
        return "操作成功";
    }

    @Override
    public Object selectHardwareByUserId(LisenceServiceQuery lisenceServiceQuery) {
        PageHelper.startPage(lisenceServiceQuery.getPageNum(), lisenceServiceQuery.getPageSize());
        List<HardwareEntity> infos = hardwareMapper.selectHardwareByUserId(lisenceServiceQuery);
        return new PageInfo<>(infos);
    }

    @Override
    public String selectHardwareNameByKey(String key) {
        List<HardwareEntity> hardwareEntities = hardwareMapper.selectHardwareNameByKey(key);
        return hardwareEntities.get(0).getHardwareName();
    }


    @Override
    public List<SoftwareEntity> selectSoftwareInfoById(Integer hardwareId) {
        return hardwareMapper.selectSoftwareInfoById(hardwareId);
    }

    @Override
    @AnnotationForLog(operationType = LogEnum.UPDATE_HARDWARE,operation = "修改硬件信息",operationalEntity = "HardwareEntity.java/STUDENT_T")
    public void updateHardware(HardwareEntity hardwareEntity,Integer userId,IncResponce incResponce) {

        String hardwareName = hardwareEntity.getHardwareName();
        if (StringUtils.isNoneBlank(hardwareEntity.getMacKey())) {
            String key=hardwareEntity.getCpuKey()+","+hardwareEntity.getHardDiskKey()+","+hardwareEntity.getMacKey();
            hardwareEntity.setHardwareKey(key);
        }
        else
        {
            String hardwareKey=hardwareEntity.getHardwareKey();
            hardwareEntity.setHardwareKey(hardwareKey);
        }
        List<HardwareEntity> hardwares = hardwareMapper.selectHardwareNameByKey(hardwareEntity.getHardwareKey());
        if (hardwares.size() >= 1 && !Objects.equals(hardwares.get(0).getId(), hardwareEntity.getId())){
            incResponce.setCode(1);
            incResponce.setErrorMessage("硬件信息重复");
            return ;
        }
        List<HardwareEntity> names = hardwareMapper.selectHardwareNameByName(hardwareName);
        if (names.size() >= 1 && !Objects.equals(names.get(0).getId(), hardwareEntity.getId())) {
            incResponce.setCode(1);
            incResponce.setErrorMessage("硬件名称不能重复");
            return;
        }
        hardwareMapper.updateHardware(hardwareEntity);
//        Integer id = hardwareEntity.getId();
//        List<Integer> softwareIds = hardwareEntity.getSoftwareIds();
//        if (softwareIds != null && softwareIds.size()>0) {
//            hardwareMapper.delConnSoftware(id);
//            for (Integer softwareId : softwareIds) {
//                HardwareConnSoftware hardwareConnSoftware = new HardwareConnSoftware();
//                hardwareConnSoftware.setHardwareId(id);
//                hardwareConnSoftware.setSoftwareId(softwareId);
//                hardwareMapper.saveConnSoftware(hardwareConnSoftware);
//            }
//        }
        incResponce.setCode(0);
        incResponce.setErrorMessage("操作成功");
    }

    @Override
    @AnnotationForLog(operationType = LogEnum.DEL_HARDWARE,operation = "删除硬件信息",operationalEntity = "HardwareEntity.java/STUDENT_T")
    public String delHardware(Integer id,Integer userId) {
        hardwareMapper.delHardware(id);
        hardwareMapper.delConnSoftware(id);
        hardwareMapper.delConnGroupingByHardwareId(id);
        return "操作成功";
    }


}
